<?php
/** 
* polymercontroller.php
* Create on 2016-08-12 11:09:22
* Create by yuebin
*/
namespace haibao\cms\data\control;


class PolymerController extends \haibao\cms\data\BaseMysql {
	public function __construct() {
		parent::__construct ( __CLASS__ );
	}

	public function getPolymerInfoList($status,$limit = 0){
		 
		$list = array();
		$sql = 'select * from PolymerInfo where `Status`=%s and ObjectType <> 5';
		if($limit){
			$sql .= ' order by Id desc limit '.$limit;
		}
		$result = $this->query($sql, array($status));
		while($rowValue = $result->fetch_assoc()){
			array_push($list,$rowValue);
		}
		return $list;
	}
	
	public function getPolymerTagList($tagId,$limit = 1){
		$polymerTagPageList = array();
		$sql = 'select * from PolymerTag where TagId=%s';
		if($limit){
			$sql .= " order by Id desc limit $limit";
		}
		$result = $this->query($sql, array($tagId));
		while($rowValue = $result->fetch_assoc()){
			array_push($polymerTagPageList,$rowValue);
		}
		return $polymerTagPageList;
	}
	
	public function getPolymerInfoCnt($tagId,$status=0){
		$sql = 'select count(*) as cnt from PolymerInfo where TagList like %s and ObjectType <> 5';
		if($status){
			$sql .= ' and `Status` <> 1';
		}else{
			$sql .= ' and `Status` = 0';
		}
		$result = $this->query($sql, array('%#'.$tagId.'#%'))->fetch_assoc();
		return $result['cnt'];
	}
	

	public function getPolymerInfoCntBetweenId($tagId,$startId,$endId){
		$sql = 'select count(*) as cnt from PolymerInfo where Id>=%s and Id<=%s and TagList like %s and ObjectType <> 5';
		$sqlVal =  array((int)$startId,(int)$endId,'%#'.$tagId.'#%');
		$result = $this->query($sql, $sqlVal)->fetch_assoc();
		return $result['cnt'];
	}
	
	public function getPolymerInfoByTagId($tagId,$status=0){
		$list = array();
		$sql = 'select * from PolymerInfo where TagList like %s and ObjectType <> 5';
		if($status){
			$sql .= ' and `Status` <> 1';
		}else{
			$sql .= ' and `Status` = 0';
		}
		$result = $this->query($sql, array('%#'.$tagId.'#%'));
		while($rowValue = $result->fetch_assoc()){
			array_push($list,(object)$rowValue);
		}
		return $list;
	}
	

	public function getPolymerInfoBetweenId($tagId,$startId,$endId,$notEqualId = 0){
		$list = array();
		if($notEqualId){
			$sql = 'select * from PolymerInfo where Id>=%s and Id<=%s and TagList like %s and Id <>%s and ObjectType <> 5';
			$sqlVal =  array((int)$startId,(int)$endId,'%#'.$tagId.'#%',$notEqualId);
		}else{
			$sql = 'select * from PolymerInfo where Id>=%s and Id<=%s and TagList like %s and ObjectType <> 5';
			$sqlVal =  array((int)$startId,(int)$endId,'%#'.$tagId.'#%');
		}
		$result = $this->query($sql,$sqlVal);
		while($rowValue = $result->fetch_assoc()){
			array_push($list,(object)$rowValue);
		}
		return $list;
	}
	
	public function getCompletePolymerInfoList($tagId, $lastPolymerInfoId, $limit){
		$list = array();
		$sql = 'select * from PolymerInfo where Id<%s and TagList like %s and ObjectType <> 5 order by Id DESC limit 0,%s';
		$result = $this->query($sql, array($lastPolymerInfoId,'%#'.$tagId.'#%',$limit));
		while($rowValue = $result->fetch_assoc()){
			array_push($list,(object)$rowValue);
		}
		return $list;
	}
	
	public function updatePolymerInfoStatus($polymerInfoId, $status){
	
		$sql = 'update PolymerInfo set Status=%s where Id=%s';
		$this->query($sql,array($status,(int)$polymerInfoId));
	}
	
	public function getPolymerTagBetweenTagIdAndPolymerId($tagId,$polymerInfoId){
		$sql = 'select * from PolymerTag where StartPolymerInfoId<=%s and EndPolymerInfoId>=%s and TagId=%s limit 1';
		return  $this->query($sql, array($polymerInfoId,$polymerInfoId,$tagId))->fetch_assoc();
	}
	public function getTagsById($tagIds){
		$str = preg_replace('/(\d+)/', '%s', implode(',', $tagIds));
		$sql = "select tag_id,tag_name from `tag`where tag_id in(".$str.")";
			
		$result = $this->query($sql, $tagIds);
		$tmp = array();
		while ($result->fetch_assoc()){
			$tmp[$result->getData('tag_id')] = $result->getData('tag_name');
		}
		return $tmp;
	}	
	public function deletePolymerInfoById($polymerInfoId){
	
		$sql = 'delete from PolymerInfo where Id=%s';
		$this->query($sql,array($polymerInfoId));
	}
	
	public function getNewPolymerInfoByPrimaryTagId($tagId,$limit=2){
		$list = array();
		$sql = 'select * from PolymerInfo where PrimaiyTagId=%s and ObjectType <> 5 and `Status` = 0 order by Id DESC';
		if($limit){
			$sql .= ' limit '.$limit.'';
		}
		$result = $this->query($sql, array($tagId));
		while($rowValue = $result->fetch_assoc()){
			array_push($list,(object)$rowValue);
		}
		return $list;
	}
	

	public function getNotesTagListByTopTagId($topTagId,$limit){
		$list = array();
		$sql = "select * from PolymerInfo where Level>3 and Status = %s and ObjectType in (%s,%s) and ParentTagId = %s order by Id desc limit %s";
		$typeArticle = \haibao\cms\model\data\PolymerInfo::OBJECT_TYPE_ARTICLE;
		$typeImage = \haibao\cms\model\data\PolymerInfo::OBJECT_TYPE_IMAGE;

		$result = $this->query($sql,array(\haibao\cms\model\data\PolymerInfo::STATUS_OK,$typeArticle,$typeImage,$topTagId,$limit));
		while($rowValue = $result->fetch_assoc()){
			array_push($list,(object)$rowValue);
		}
		return $list;
	}

	public function getPolymerListByParentTagId($topTagId,$objectType,$limit=20){
		$str = preg_replace('/(\d+)/', '%s', implode(',', $objectType));
		$sql = "select count(*) as Cnt,PrimaiyTagId from PolymerInfo where ParentTagId = %s and ObjectType in (".$str.") group by PrimaiyTagId order by cnt desc limit %s";
		$params = array_merge(array($topTagId),$objectType);
		$result = $this->query($sql,array_merge($params,array($limit)));
		$mostTagList = $resArr = array();
		while($result->fetch_assoc()){
			$mostTagList[$result->getData('PrimaiyTagId')] = $result->getData('Cnt');
		}
		$tagInfos = $this->getTagsById(array_keys($mostTagList));
		$resArr = array();
		foreach($mostTagList as $key=>$tag){
		    if(isset($tagInfos[$key])){
		        $resArr[] = array('Id'=>$key,'TagName'=>$tagInfos[$key]);
		    }
		}
		return $resArr;
	}
}